<?
include_once('db_setup.php');
$table = $_REQUEST['table'];
//$table = 'outdoor_places':
$ctrlat = $_REQUEST['ctrlat'];
$ctrlon = $_REQUEST['ctrlon'];
$swlat = $_REQUEST['swlat'];
$swlon = $_REQUEST['swlon'];
$nelat = $_REQUEST['nelat'];
$nelon = $_REQUEST['nelon'];
$count = $_REQUEST['count'];

// Cap # of rows returned
if($count>50) 	$count = 50;  

$type = $_REQUEST['type'];
$query = "SELECT *,round(jd_distance($ctrlat,$ctrlon,lat,lon),0) as distance from $table where lat<$nelat and lon<$nelon and lat>$swlat and lon>$swlon order by jd_distance($ctrlat,$ctrlon,lat,lon) asc limit $count";
$result = mysql_query($query); 
$rows = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
//	$temp = json_decode($row['json'], true);
//	array_push($row,$temp);
	$output[] = $row;
//	$output[] = $temp;
	$rows = $rows + 1;
}
$data = json_encode($output);  //encode the data in json format
echo '({"total":"' . $rows . '","results":' . $data . '})';
?>
